package com.casework

import org.joda.time.LocalDate
import org.joda.time.LocalDateTime
import static com.casework.CompetitionConcern.*
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil

class LegalCase {
    Long ecNumber
    LocalDate notificationDate
    LocalDate decisionDate
    LocalDateTime lastUpdated

    Integer phase
    YesNoNoData ph1Offer
    LocalDate dateOfStatementOfObjections
    RelativeToSO relativeToSO
    TypeA typeA
    TypeB typeB
    String commentOnOffers

    //Todo not sure how to handle links to decisions
    String mLexLink
    String pressReleaseExternallink
    String pressReleaseCompRXServerlink
    String decisionExternallink

    DealType dealType
    List offers = []
    List measures = []
    List files = []

    static hasMany = [offers: Offer, measures: Measure, files: AttachedFile]
    static transients = ['competitionConcern', 'article', 'ecNumbers', 'allDecisionDates', 'industry', 'firms']
    static searchable = {
        only = ['ecNumber', 'commentOnOffers']
    }

    static constraints = {
        ecNumber nullable: false
        notificationDate nullable: true
        decisionDate nullable: true

        ph1Offer nullable: true
        dateOfStatementOfObjections nullable: true
        relativeToSO nullable: true
        typeA nullable: true
        typeB nullable: true
        commentOnOffers nullable: true

        mLexLink nullable: true
        pressReleaseExternallink nullable: true
        pressReleaseCompRXServerlink nullable: true
        decisionExternallink nullable: true
        offers display: false
        measures display: false
        lastUpdated display: false, nullable: true
        dealType nullable: true
    }

    static mapping = {
        commentOnOffers type: 'text'
    }

    def getIndustry() {
        def descriptions = LegalCaseNaceFullDescription.findAllByLegalCase(this).collect { it.description } as Set
        descriptions.sort {a, b -> a.code <=> b.code}.join(', ')
    }

    def getFirms() {
        def firms = LegalCaseFirm.findAllByLegalCase(this).collect { it.firm } as Set
        return firms.sort {a, b -> a.name <=> b.name}.join(', ')
    }

    static def getEcNumbers() {
        def cases = LegalCase.withCriteria() {
            projections {
                'ecNumber'
            }
            order 'ecNumber', 'desc'
            cache true
        }
        return cases.collect {"M.${it.ecNumber}"}
    }

    static def getAllDecisionDates() {
        def cases = LegalCase.withCriteria() {
            projections {
                'decisionDate'
            }
            cache true
        }
        def years = cases.collect {
            if(!it.decisionDate){
                return 2000
            }
            it.decisionDate.year
        }
        return years.sort() as Set
    }


    String getArticle() {
        switch (phase) {
            case null:
                return ''
            case 1:
                return '6(1)(b)'
            case 2:
                return '8(2)'
            default:
                return ''
        }
    }

    String getCompetitionConcern() {
        def groups = measures.groupBy {
            it?.competitionConcern
        }
        if (groups.size() >= 2) {
            return Mixed.toString()
        } else if (groups.size() == 0) {
            return "Unclassified as no Measure"
        }

        return groups.keySet().find {true}.toString()
    }


    def beforeCreate() {
        lastUpdated = new LocalDateTime()
    }

    def beforeUpdate() {
        lastUpdated = new LocalDateTime()
    }

    void beforeDelete() {
        LegalCase.withNewSession {
            def caseFirm = LegalCaseFirm.findAllByLegalCase(this)
            caseFirm*.delete()

            def caseNaceFullDescription = LegalCaseNaceFullDescription.findAllByLegalCase(this)
            caseNaceFullDescription*.delete()
        }
    }

    @Override
    String toString() {
        return ecNumber
    }

}
